package com.example.test_activity.http;

import com.example.test_activity.base.Url;

import java.net.CookieManager;

import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 旭 on 2017/5/4/004.
 */

public class HttpUtil {

    private Retrofit retrofit;
    private ApiServer server;
    private OkHttpClient okHttpClient;
    private OkHttpClient.Builder builder = new OkHttpClient.Builder();
    private static HttpUtil INSTANCE;

    private HttpUtil(){
        CookieManager manager = new CookieManager();
        builder.cookieJar(new JavaNetCookieJar(manager));
        okHttpClient = builder.build();
    }

//    private static class HttpUtilHolder{
//        private static HttpUtil INSTANCE = new HttpUtil();
//
//    }
//    //获得单例
//    public static HttpUtil getInstance()
//    {
//        return HttpUtilHolder.INSTANCE;
//    }

    public static HttpUtil getInstance()
    {
        if (INSTANCE == null)
        {
            INSTANCE = new HttpUtil();
        }

        return INSTANCE;
    }

    public ApiServer sendGetPost()
    {
        retrofit = new Retrofit.Builder()
                .baseUrl(Url.URL)
                .client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        server = retrofit.create(ApiServer.class);
        return server;
    }

    public void RestHttpUtil()
    {
       INSTANCE = new HttpUtil();
    }

}
